﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FTBS.Models.BLO;
using FTBS.Models.Entities;
using FTBS.Models.Entities.Concert;
using PagedList;

namespace FTBS.Controllers
{
    public class PriceTicketController : Controller
    {
        //
        // GET: /PriceTicket/
        private PriceTicketBLO _priceTicketBlo = new PriceTicketBLO();
        private PriceTicketConcertEntityBLO _priceTicketConcertEntityBlo = new PriceTicketConcertEntityBLO();
        private TheaterBLO _theaterBlo = new TheaterBLO();
        private ChairTypeBLO _chairTypeBlo = new ChairTypeBLO();
        public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page, bool? isSortDescending)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }
            List<PriceTicketConcertEntity> listPriceTicket = new List<PriceTicketConcertEntity>();
            listPriceTicket = _priceTicketConcertEntityBlo.LoadPriceTickets();
            List<ChairType> chairTypes = _chairTypeBlo.LoadChairTypes();
            if (chairTypes != null)
            {
                for (int i = 0; i < chairTypes.Count; )
                {
                    ViewBag.TypeName1 = chairTypes[i].TypeName;
                    i++;
                    ViewBag.TypeName2 = chairTypes[i].TypeName;
                    i++;
                    ViewBag.TypeName3 = chairTypes[i].TypeName;
                    i++;
                }
            }
            int pageSize = 5;
            int pageNumber = (page ?? 1);

            return View(listPriceTicket.ToPagedList(pageNumber, pageSize));
        }


        public ActionResult Create()
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }
            ViewBag.Theater = _theaterBlo.GetAllTheaterInfoToCreatePriceTicket();
            ViewBag.ChairType = _chairTypeBlo.LoadChairTypes();
            return View();
        }

        public string SubmitCreate(int theaterId, float p1, float p2, float p3, float sp1, float sp2, float sp3)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return "SessionTimeOut";
            }

            List<PriceTicket> listPriceTicket = new List<PriceTicket>();
            if (!_priceTicketBlo.CheckIsExistedPriceTicket(theaterId))
            {
                PriceTicket priceTicketSingle = new PriceTicket();
                priceTicketSingle.TheaterId = theaterId;
                priceTicketSingle.Price = p1;
                priceTicketSingle.SalePrice = sp1;
                priceTicketSingle.ChairTypeId = 1;
                listPriceTicket.Add(priceTicketSingle);

                PriceTicket priceTicketVip = new PriceTicket();
                priceTicketVip.TheaterId = theaterId;
                priceTicketVip.Price = p2;
                priceTicketVip.SalePrice = sp2;
                priceTicketVip.ChairTypeId = 2;
                listPriceTicket.Add(priceTicketVip);

                PriceTicket priceTicketCouple = new PriceTicket();
                priceTicketCouple.TheaterId = theaterId;
                priceTicketCouple.Price = p3;
                priceTicketCouple.SalePrice = sp3;
                priceTicketCouple.ChairTypeId = 3;
                listPriceTicket.Add(priceTicketCouple);

                var result = _priceTicketBlo.CreatePriceTicket(listPriceTicket);
                if (result)
                {
                    return "createSuccessful";
                }
            }
            return "false";
        }

        public ActionResult Edit(int id, string theaterName)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }

            ViewBag.theaterName = theaterName;
            ViewBag.PriceTicket = _priceTicketConcertEntityBlo.LoadPriceTicketsByTheaterId(id);
            return View();
        }

        public string SubmitEdit(int theaterId, float p1, float p2, float p3, float sp1, float sp2, float sp3)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return "SessionTimeOut";
            }
            List<PriceTicket> listBoughtPriceTickets = new List<PriceTicket>();
            List<PriceTicket> listPriceTicket = new List<PriceTicket>();
            
            PriceTicket priceTicketSingle = new PriceTicket();
            priceTicketSingle.TheaterId = theaterId;
            priceTicketSingle.Price = p1;
            priceTicketSingle.SalePrice = sp1;
            priceTicketSingle.ChairTypeId = 1;
            
            PriceTicket priceTicketVip = new PriceTicket();
            priceTicketVip.TheaterId = theaterId;
            priceTicketVip.Price = p2;
            priceTicketVip.SalePrice = sp2;
            priceTicketVip.ChairTypeId = 2;
            
            PriceTicket priceTicketCouple = new PriceTicket();
            priceTicketCouple.TheaterId = theaterId;
            priceTicketCouple.Price = p3;
            priceTicketCouple.SalePrice = sp3;
            priceTicketCouple.ChairTypeId = 3;
            
            //get price ticket in database of single chair
            priceTicketSingle.PriceTicketId = _priceTicketBlo.GetPriceTicketId(priceTicketSingle.TheaterId, priceTicketSingle.ChairTypeId);
            PriceTicket priceTicketSingleInDb =
                _priceTicketBlo.GetPriceTicketById(_priceTicketBlo.GetPriceTicketId(theaterId,
                                                                                    priceTicketSingle.ChairTypeId));
            //get price ticket in database of vip chair
            priceTicketVip.PriceTicketId = _priceTicketBlo.GetPriceTicketId(priceTicketVip.TheaterId, priceTicketVip.ChairTypeId);
            PriceTicket priceTicketVipInDb =
                _priceTicketBlo.GetPriceTicketById(_priceTicketBlo.GetPriceTicketId(theaterId,
                                                                                    priceTicketVip.ChairTypeId));

            //get price ticket in database of couple chair
            priceTicketCouple.PriceTicketId = _priceTicketBlo.GetPriceTicketId(priceTicketCouple.TheaterId, priceTicketCouple.ChairTypeId);
            PriceTicket priceTicketCoupleInDb =
                _priceTicketBlo.GetPriceTicketById(_priceTicketBlo.GetPriceTicketId(theaterId,
                                                                                    priceTicketCouple.ChairTypeId));

            if (priceTicketSingle.SalePrice == priceTicketSingleInDb.SalePrice &&
                priceTicketVip.SalePrice == priceTicketVipInDb.SalePrice &&
                priceTicketCouple.SalePrice == priceTicketCoupleInDb.SalePrice)
            {
                if (priceTicketSingle.Price > priceTicketSingleInDb.Price ||
                    priceTicketSingle.Price < priceTicketSingleInDb.Price)
                {
                    listBoughtPriceTickets.Add(priceTicketSingle);
                }
                if (priceTicketVip.Price > priceTicketVipInDb.Price ||
                    priceTicketVip.Price < priceTicketVipInDb.Price)
                {
                    listBoughtPriceTickets.Add(priceTicketVip);
                }
                if (priceTicketCouple.Price > priceTicketCoupleInDb.Price ||
                    priceTicketCouple.Price < priceTicketCoupleInDb.Price)
                {
                    listBoughtPriceTickets.Add(priceTicketCouple);
                }
            }
            else
            {
                if (priceTicketSingle.SalePrice > priceTicketSingleInDb.SalePrice ||
                    priceTicketSingle.SalePrice < priceTicketSingleInDb.SalePrice)
                {
                    listPriceTicket.Add(priceTicketSingle);
                }
                if (priceTicketVip.SalePrice > priceTicketVipInDb.SalePrice ||
                    priceTicketVip.SalePrice < priceTicketVipInDb.SalePrice)
                {
                    listPriceTicket.Add(priceTicketVip);
                }
                if (priceTicketCouple.SalePrice > priceTicketCoupleInDb.SalePrice ||
                    priceTicketCouple.SalePrice < priceTicketCoupleInDb.SalePrice)
                {
                    listPriceTicket.Add(priceTicketCouple);
                }
            }

            if (listBoughtPriceTickets.LongCount() == 0 && listPriceTicket.LongCount() == 0)
            {
                return "editSuccessful";
            }
            else if (listBoughtPriceTickets.LongCount() != 0 && listPriceTicket.LongCount() == 0)
            {
                var result = _priceTicketBlo.EditBoughtPriceTickets(listBoughtPriceTickets);
                if (result)
                {
                    return "editSuccessful";
                }
                return "false";
            }
            else if (listPriceTicket.LongCount() != 0)
            {
                var result = _priceTicketBlo.EditPriceTicket(listPriceTicket);
                if (result)
                {
                    return "editSuccessful";
                }
                return "false";
            }
            return "false";
        }

        [HttpDelete, ActionName("Delete")]
        public ActionResult Delete(int id = 0)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }
            PriceTicket pt = _priceTicketBlo.GetPriceTicketById(id);
            if (_priceTicketBlo.IsPriceTicketUsedForSchedule(pt))
            {
                TempData["CanNotDelete"] = true;
                return RedirectToAction("Index");
            }
            else
            {
                if (pt == null)
                {
                    HttpNotFound();
                }
                _priceTicketBlo.DeletePriceTicket(id);
                return RedirectToAction("Index");
            }

        }
    }
}
